<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016-10-12
 * Time: 10:31
 */

/**
 * 分页函数
 * @param $list查询的分页数组
 * @acthor:式神 289650682@qq.com
 * @return 2016-10-12
 */
function page_nav($list,$num=8)
{
    //半个页码
    $page0=floor($num/2);
    //$page1=floor($num/4);
    $html = '<div class="page_nav"><div><a class="first" href="' . $list->url(1) . '">首页</a>';
    if (!$list->previousPageUrl()) {
        $html .= '<span class="prev">上一页</span>';
    } else {
        $html .= '<a class="prev" href="' . $list->previousPageUrl() . '">上一页</a>';
    }

    if($list->lastPage()>$page0){
        if($list->currentPage()-2>0 && $list->currentPage()+2<=$list->lastPage()){
            for ($i = $list->currentPage()-2; $i <= $list->currentPage()+2; $i++){
                if($list->currentPage()==$i){
                    $html .= '<span class="current">'. $i .'</span>';
                }else{
                    $html .= '<a href="'. $list->url($i) .'">'. $i .'</a>';
                }
            }
        }elseif($list->currentPage()-1>0 && $list->currentPage()+3<=$list->lastPage()){
            for ($i = $list->currentPage()-1; $i <= $list->currentPage()+3; $i++){
                if($list->currentPage()==$i){
                    $html .= '<span class="current">'. $i .'</span>';
                }else{
                    $html .= '<a href="'. $list->url($i) .'">'. $i .'</a>';
                }
            }
        }elseif($list->currentPage()>0 && $list->currentPage()+4<=$list->lastPage()){
            for ($i = $list->currentPage(); $i <= $list->currentPage()+4; $i++){
                if($list->currentPage()==$i){
                    $html .= '<span class="current">'. $i .'</span>';
                }else{
                    $html .= '<a href="'. $list->url($i) .'">'. $i .'</a>';
                }
            }
        }elseif($list->currentPage()-3>0 && $list->currentPage()+1<=$list->lastPage()){
            for ($i = $list->currentPage()-3; $i <= $list->currentPage()+1; $i++){
                if($list->currentPage()==$i){
                    $html .= '<span class="current">'. $i .'</span>';
                }else{
                    $html .= '<a href="'. $list->url($i) .'">'. $i .'</a>';
                }
            }
        }elseif($list->currentPage()-4>0 && $list->currentPage()<=$list->lastPage()){
            for ($i = $list->currentPage()-4; $i <= $list->currentPage(); $i++) {
                if ($list->currentPage() == $i) {
                    $html .= '<span class="current">'. $i .'</span>';
                } else {
                    $html .= '<a href="'. $list->url($i) .'">'. $i .'</a>';
                }
            }
        }
    }else{
        for ($i = 1; $i <= $list->lastPage(); $i++){
            if($list->currentPage()==$i) {
                $html .= '<span class="current">' . $i . '</span>';
            }else{
                $html .= '<a href="'.$list->url($i).'">'. $i .'</a>';
            }
        }
    }

    if (!$list->nextPageUrl()){
        $html .= '<span class="next" > 下一页</span >';
    }else{
        $html .= '<a class="next" href = "'.$list->nextPageUrl().'" > 下一页</a >';
    }
    $html .= '<a class="end" href="'.$list->url($list->lastPage()).'">最后一页</a>';
    $html .= '<span class="rows">当前页有 '.$list->count().' 条记录</span>';
    $html .= '<span class="rows">共 '.$list->total().' 条记录</span>';
    $html.='</div></div>';
    return $html;
}

/**
 * 多级分类函数
 * @param
 * @acthor:式神 289650682@qq.com
 * @return 2016-10-12
 */
function catlist($cate,$aidname='c_id',$catename='_cate_name',$aid=0,$html1='|',$html2='—',$level=0){
    $arr=array();
    $html='';
    foreach($cate as $k=>$v){
        if($v->$aidname==$aid){
            $v['level']=$level+1;
            if($level==1){
                $html=$html1.$html2;
            }
            if($level>1){
                $html=$html1.str_repeat($html2,$level);;
            }
            $v[$catename]=$html.$v->name;
            $arr[]=$v;
            $v['level']=$level+1;
            $arr=array_merge($arr,catlist($cate,$aidname,$catename,$v->id,$html1,$html2,$level+1));
        }
    }
    return $arr;
}

/**
 * 多级分类树形
 * @param
 * @acthor:式神 289650682@qq.com
 * @return 2016-10-12
 */
function catTree($cate,$aidname='c_id',$aid=0){
    $arr=array();
    foreach($cate as $k=>$v){
        if($v->$aidname==$aid){
            $v['child']=catTree($cate,$aidname,$v->id);
            $arr[]=$v;
        }
    }
    return $arr;
}

/**
 * 多级分类树形基于zTree
 * @param
 * @acthor:式神 289650682@qq.com
 * @return 2016-10-12
 */
function catzTree($cate,$aidname='c_id',$groupPermissions=array(),$beal=true){
    $arr=array();
    //{ id:111, pId:11, name:"随意勾选 1-1-1"},
    $permission=array();
    if(count($groupPermissions)>0){
        foreach ($groupPermissions as $key => $value) {
            $permission[]=$key;
        }
    }
    foreach($cate as $v){
        $v['open']=$beal;
        if(in_array($v['role'],$permission)){
            $v['checked']=true;
        }
        $arr[]=$v;
    }
    return $arr;
}

/**
 * 查询子栏目id
 * @acthor:式神 2896506822qq.com
 * @time: 2016-10-12
 */
//子栏目ID
function getChildsId($cate,$pid=0,$aidname='a_id'){
    $arr=array();
    foreach ($cate as $v) {
        if($v->$aidname==$pid){
            $arr[]=$v->id;
            $arr= array_merge($arr,getChildsId($cate,$v->id,$aidname));
        }
    }
    return $arr;
}
